package com.gxa.shop.dao.history.Impl;

import com.gxa.shop.bean.pojo.History;
import com.gxa.shop.bean.vo.FavoriteVo;
import com.gxa.shop.bean.vo.HistoryVo;
import com.gxa.shop.commons.jdbc.JDBCUtil;
import com.gxa.shop.dao.history.HistoryDao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * com.gxa.shop.dao.history.Impl
 * User: 刁彦侨
 * Date: 2024/12/24 09:39
 * motto:   逆水行舟不进则退
 * Description:
 * Version: V1.0
 */
public class HistoryDaoImpl implements HistoryDao {
    @Override
    public List<HistoryVo> findById(String id) {
        String sql = "SELECT h.user_id, p.product_id, p.product_name, p.product_image, p.product_color, p.unit_price FROM browse_history h JOIN product p ON h.product_id = p.product_id WHERE h.user_id = ? ORDER BY h.history_id DESC LIMIT 8";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Map<String, Object>> query = jdbcUtil.query(sql.toString(), id);
        if(query.size()>0){
            List<HistoryVo> historys = new ArrayList<>();
            for (Map<String, Object> row : query) {
                HistoryVo historyVo = new HistoryVo();
                historyVo.setUserId((Integer) row.get("user_id"));
                historyVo.setProductId((Integer) row.get("product_id"));
                historyVo.setProductName((String) row.get("product_name"));
                historyVo.setProductImage((String) row.get("product_image"));
                historyVo.setProductColor((String) row.get("product_color"));
                historyVo.setUnitPrice((Double) row.get("unit_price"));
                historys.add(historyVo);
            }
            System.out.println("历史浏览查询成功");
            return historys;
        }
        System.out.println("历史浏览为空");
        return null;
    }

    @Override
    public int insert(History history) {
        String sql = "INSERT INTO browse_history(user_id, product_id) VALUES (?, ?)";
        JDBCUtil jdbcUtil = new JDBCUtil();
        List<Object> parameters=new ArrayList<>();
        parameters.add(history.getUserId());
        parameters.add(history.getProductId());
        return jdbcUtil.update(sql,parameters.toArray());
    }

    @Override
    public int deleteById(Integer userId, Integer productId) {
        String sql = "DELETE FROM browse_history WHERE user_id = ? AND product_id = ?";
        JDBCUtil jdbcUtil = new JDBCUtil();
        return jdbcUtil.update(sql, userId, productId);
    }
}
